package com.app.micaihu.view.main.topic.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.topic.TopicInfo;
import com.app.micaihu.configure.AppConfigTools;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.listener.HttpListener;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * 全部话题列表适配器
 * Created by linkai on 2016/9/9.
 */
public class TopicAdapter extends AppBaseAdapter<TopicInfo> implements View.OnClickListener {

    public TopicAdapter(List<TopicInfo> list, Context context) {
        super(list, context);
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_topic_hot,null);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) convertView.findViewById(R.id.item_public_title);
            viewHolder.img = (CustomImageView) convertView.findViewById(R.id.item_small_image);
            viewHolder.fllow = (TextView) convertView.findViewById(R.id.attention);
            viewHolder.fllow.setOnClickListener(this);

//            ViewSizeUtils.changeViewAuto1080(viewHolder.img, 280, 200);
//            ViewSizeUtils.changeViewAuto1080(viewHolder.img, 400, 300);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TopicInfo topicInfor = list.get(position);
        if(topicInfor == null){
            return convertView;
        }
        LoadImageUtils.getInstance().loadWifiImage(viewHolder.img, topicInfor.getThumb());
        viewHolder.title.setText("#" + topicInfor.getTitle() + "#");
        viewHolder.fllow.setTag(topicInfor);
        if(topicInfor.getIsAttention()){
            //已关注
            viewHolder.fllow.setSelected(false);
            viewHolder.fllow.getCompoundDrawables()[0].setLevel(5);
            viewHolder.fllow.setText(context.getString(R.string.homepage_attentioned));
        }else{
            //未关注
            viewHolder.fllow.setSelected(true);
            viewHolder.fllow.getCompoundDrawables()[0].setLevel(4);
            viewHolder.fllow.setText(context.getString(R.string.homepage_attention));
        }
        return convertView;
    }

    @Override
    public void onClick(View v) {

        //关注
        if(!UserInforManager.getInstance().isLogin()){
            UserInforManager.getInstance().toLoginPage((Activity) context);
            return;
        }
        attentionTopic(v);
    }

    /**
     * 关注话题
     */
    private void attentionTopic(final View  view) {
        final TopicInfo topicInfo = (TopicInfo) view.getTag();
        if(topicInfo == null){
            return;
        }
        final boolean isAttention = topicInfo.getIsAttention();
        ParamMap params = new ParamMap();
        params.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());
        params.putEncode("topicId", topicInfo.getTopicId());
        if (!isAttention) {
        //未关注->去关注
            params.putEncode("type", "1");
            StatService.onEvent(context, "043", "关注话题", 1);//统计事件的点击次数
        } else {
        //已关注->取消关注
            params.putEncode("type", "2");
            StatService.onEvent(context, "043", "取消关注", 1);//统计事件的点击次数
        }

        HttpUtils.ParmsMapMD5(params);

        HttpListener<DataBean> attentionListener = new HttpListener<DataBean>() {

            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onError(VolleyError error) {
                ToastUtils.makeText(context.getString(R.string.neterror));
            }

            @Override
            public void onSuccess(DataBean response) {
                DialogUtils.getInstance().dismissProgressDialog();
                if (response.noError()) {
                    if (isAttention) {//当前是未关注，则变为已关注
//                        view.setTag(R.id.tag_first, false);
                        topicInfo.setIsAttention("0");

                    } else {
//                        view.setTag(R.id.tag_first, true);
                        topicInfo.setIsAttention("1");

                    }
                    view.setTag(topicInfo);
//                    if (!isAttention) {
//                        ((TextView)view).setSelected(false);
//                        ((TextView)view).getCompoundDrawables()[0].setLevel(5);
//                        ((TextView)view).setText(context.getString(R.string.homepage_attentioned));
//                    } else {
//                        ((TextView)view).setSelected(true);
//                        ((TextView)view).getCompoundDrawables()[0].setLevel(4);
//                        ((TextView)view).setText(context.getString(R.string.homepage_attention));
//                    }
                    if(topicInfo.getIsAttention()){//1.已关注
                        ((TextView)view).setSelected(false);
                        ((TextView)view).getCompoundDrawables()[0].setLevel(5);
                        ((TextView)view).setText(context.getString(R.string.homepage_attentioned));
                    }else{//未关注
                        ((TextView)view).setSelected(true);
                        ((TextView)view).getCompoundDrawables()[0].setLevel(4);
                        ((TextView)view).setText(context.getString(R.string.homepage_attention));
                    }
                    AppConfigTools.topicAttention = true;//刷新关注状态（用于我的关注页面刷新）

                } else {
                    ToastUtils.makeText(response.getNnderstoodMsg());
                }
            }
        };

        HttpUtils.addPostRequest(URIContant.TOPIC_ATTENTION, new TypeToken<DataBean>() {
        }.getType(), null, params, attentionListener);
    }

    private class ViewHolder{
        CustomImageView img;
        TextView title;
        TextView fllow;
    }
}
